<template>
  <div  class="wrapper">
    <div style="height:300px">
      <FormPanel  
        ref="from"
        :list="initForm"   
        :cols="3"
        :formType="value.type"
        :labelWidth="120"
        :isSubmitBtn="false"
        :collapse="false"
        @formChange="changeHandle"
      />
    </div>

    <div class="dialogBottmView">
      <el-button @click="closeDialog('closeDialog')">返 回</el-button>
      <el-button :loading="loadingFlag" @click="saveDialog" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { getAreaData } from '@/api/serviceass/pdimanage/area';
import { postSave, getCheckingProjectData } from '@/api/serviceass/pdimanage/project'; 
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'servicessbpdimanageprojectinfo',
  mixins: [dictionary],
  props:{
    value: {
      type: Object,
      data: Object,
      required: true
    },
  },
  data() {
    this.saveData=null;
    return {
      type: this.value.type,
      loadingFlag: false,
      initForm : this.initFormFun(),
      id: '',
      projectsList: []
    }
  },
  created() {
    this.init(this.value.data)
    this.getQyList()
  },
  methods: {
    init(data) {
      if(this.type==='edit'){
        this.id = data.id
        this.initForm.forEach(x => {
          x.initialValue = data[x.fieldName];
        });
      }
    },
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: 'PDI类型',
          fieldName: 'fwjclx',
          placeholder: '请选择',
          initialValue: "",
          itemList: this.createDictList('5009'),
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {
          type: 'SELECT',
          label: '区域:',
          fieldName: 'fwjcqy',
          placeholder: '请选择...',
          itemList: [],
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'xtqybz',
          placeholder: '请选择',
          initialValue: "",
          itemList: this.createDictList('1003'),
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
        {
          type: 'TEXT_AREA',
          label: '检查项目',
          fieldName: 'fwjcxm',
          placeholder: '请输入',
          selfCols:3,
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
      ]
    },
    changeHandle(val){
      this.saveData=val
    },
    async getQyList() {
      let res=await getAreaData({ "xtqybz": '10031001' })
      this.initForm.find(x=>x.fieldName=='fwjcqy').itemList=res.data.records.map(x=>({text:x.fwqymc,value:x.id}))
    },
    closeDialog(type) {
      this.$emit('close', {visible: false, type: type});
    },
    async saveDialog() {
      console.info('save')
      this.$refs.from.SUBMIT_FORM();
      if (this.saveData) {
        this.saveData.id = this.id
        console.info(this.saveData)
        this.loadingFlag = true;
        this.projectsList = []
        var data = await getCheckingProjectData({ "fwjclx": this.saveData.fwjclx, "isId" : this.saveData.id, "fwjcqy" : this.saveData.fwjcqy, "fwjcxm" : this.saveData.fwjcxm })
        if (data.resultCode === 200) {
          this.projectsList = data.data.records
          if (this.projectsList !== null && this.projectsList !== undefined && this.projectsList !== [] && this.projectsList.length > 0) {
            this.loadingFlag = false;
            this.$notify.success({title: '提示信息',message:'保存失败，已存在的区域!'});
          } else {
            let res = await postSave(this.saveData)
            console.info(res)
            if (res.resultCode === 200) {
              this.$notify.success({title: '提示信息',message:'保存成功!'});
              this.loadingFlag = false;
              this.closeDialog('refresh')
            } else {
              this.loadingFlag = false;
              this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
            }
          }
        }
      }
    }
  }
}
</script>